/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.user.platform.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.user.comment.model.UserBalance;
import com.yami.shop.common.annotation.SysLog;
import javax.validation.Valid;

import com.yami.shop.user.comment.model.UserBalanceLog;
import com.yami.shop.user.comment.model.UserScoreLog;
import com.yami.shop.user.comment.service.UserBalanceService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import java.util.List;


/**
 * 余额充值级别表
 *
 * @author YXF
 * @date 2020-09-08 10:42:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/userBalance" )
public class UserBalanceController {

    private final UserBalanceService userBalanceService;

    /**
     * 分页查询
     * @param userBalance 余额充值级别表
     * @return 列表数据
     */
    @GetMapping("/getList" )
    public ResponseEntity<List<UserBalance>> getUserBalancePage(UserBalance userBalance) {
        return ResponseEntity.ok(userBalanceService.getBalanceList());
    }

    /**
     * 通过id查询余额充值级别表
     * @param balanceId id
     * @return 单个数据
     */
    @GetMapping("/info/{balanceId}" )
    public ResponseEntity<UserBalance> getById(@PathVariable("balanceId") Long balanceId) {
        return ResponseEntity.ok(userBalanceService.getBalanceInfo(balanceId));
    }

    /**
     * 新增余额充值级别表
     * @param userBalance 余额充值级别表
     * @return 是否新增成功
     */
    @SysLog("新增余额充值级别表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user:userBalance:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid UserBalance userBalance) {
        userBalanceService.saveBalance(userBalance);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改余额充值级别表
     * @param userBalance 余额充值级别表
     * @return 是否修改成功
     */
    @SysLog("修改余额充值级别表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user:userBalance:update')" )
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid UserBalance userBalance) {
        userBalanceService.updateBalanceById(userBalance);
        userBalanceService.removeCacheByBalanceId(userBalance.getBalanceId());
        return ResponseEntity.ok().build();
    }

    /**
     * 通过id删除余额充值级别表
     * @param balanceId id
     * @return 是否删除成功
     */
    @SysLog("删除余额充值级别表" )
    @DeleteMapping("/{balanceId}" )
    @PreAuthorize("@pms.hasPermission('user:userBalance:delete')" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long balanceId) {
        userBalanceService.removeById(balanceId);
        userBalanceService.removeCacheByBalanceId(balanceId);
        return ResponseEntity.ok().build();
    }

    /**
     * 平台批量修改会员余额
     */
    @PutMapping("/updateUserBalance")
    @PreAuthorize("@pms.hasPermission('platform:user:updateBalance')" )
    public ResponseEntity<Boolean> batchUpdateUserBalance(@RequestBody UserUpdateParam param) {
        return ResponseEntity.ok(userBalanceService.batchUpdateUserBalance(param));
    }

    /**
     * 分页查询某个用户的余额明细
     */
    @GetMapping("/getPageByUserId" )
    public ResponseEntity<IPage<UserBalanceLog>> getPageByUserId(PageParam<UserBalanceLog> page, String userId) {
        IPage<UserBalanceLog> res = userBalanceService.getPageByUserId(page,userId);
        return ResponseEntity.ok(res);
    }


}
